/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/**
 * @param {ListNode} head
 * @param {number} n
 * @return {ListNode}
 */

// 使用双指针的方法，虚拟头节点，fast移动n次。这样fast和slow的距离相差n，当fast到null时，此时slow到达倒数n节点的前驱节点，此时可删除。
var removeNthFromEnd = function(head, n) {
    let s = new ListNode(0,head);
    let slow = s,fast = s;
    let count = n;

    while(count--) {
        fast = fast.next;
    }
    while(fast.next!=null) {
        fast = fast.next;
        slow = slow.next;
    } 
    slow.next = slow.next.next;
    return s.next;
};